package com.cljz.makepassenger.present.fragment;

import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.GetUserInfoBean;
import com.cljz.makepassenger.ui.fragment.MainMenuFragment;
import com.clzj.makepassenger.bean.UserInfo;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/24.
 */

public class MainMenuP extends BaseP<MainMenuFragment> {
    public void getUserInfo() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getUserInfoData(requestBody)
                .compose(XApi.<GetUserInfoBean>getApiTransformer())
                .compose(XApi.<GetUserInfoBean>getScheduler())
                .compose(getV().<GetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetUserInfoBean getUserInfoBean) {
                        if ("0".equals(getUserInfoBean.getError_code())) {
                            GetUserInfoBean.DataBean dataBean = getUserInfoBean.getData().get(0);
                            UserInfo userInfo = getLocalUserInfo(getToken());
                            userInfo.setBirthday(dataBean.getBirthday());
                            userInfo.setUser_logo(dataBean.getUser_logo());
                            userInfo.setUser_nickname(dataBean.getUser_nickname());
                            userInfo.setUser_phone(dataBean.getUser_phone().substring(0, 3) + "****" + dataBean.getUser_phone().substring(7));
                            userInfo.setUser_sex(dataBean.getUser_sex());
                            userInfo.setEid(dataBean.getEid());
                            userInfo.setAge(dataBean.getAge());
                            userInfo.setPayment_password(dataBean.getPayment_password());
                            userInfo.setAlipay(dataBean.getAlipay());
                            userInfo.setUser_name(dataBean.getUser_name());
                            userInfo.setUser_points(dataBean.getUser_points());
                            userInfo.setRatio(dataBean.getRatio());
                            updateDB(userInfo);
                            getV().getUserInfoSucceed(getUserInfoBean.getData().get(0));
                        } else if ("1000".equals(getUserInfoBean.getError_code())) {
                            ((BaseActivity) getV().getActivity()).loginOvertime(1);
                        } else {
                            getV().getUserInfoFail(getUserInfoBean.getMsg());
                        }
                    }
                });
    }
}
